<?php
class WebMsgInfo extends AppModel {
	var $name = "WebMsgInfo";
	var $useTable = "web_msg_info";
	var $primaryKey = 'id';
	var $belongsTo = array(
			'ScoutMsgReactionInfo'=>array(
					'className'=>'ScoutMsgReactionInfo', 'conditions'=>"", 'order'=>'',
					'foreignKey'=>'scout_msg_id'
			),
			'CompanyAccount'=>array(
					'className'=>'CompanyAccount', 'conditions'=>"", 'order'=>'',
					'foreignKey'=>'company_account_id'
			),
			'Company'=>array(
					'className'=>'Company', 'conditions'=>"", 'order'=>'',
					'foreignKey'=>'company_id'
			)
	);

	function msgValid($Valid, $data) {
		$message = '';
		if ($data['status'] < 2 && isset($data['WebMsgInfo']['subject'])) {
			$message .= $Valid->ErrorMsg($data, 'WebMsgInfo/subject', 'required');
			$message .= $Valid->ErrorMsg($data, 'WebMsgInfo/subject', 'mbmaxlength', '100');
		}
		if (empty($data['status']) || $data['status']=='3') {
			$message .= $Valid->ErrorMsg($data, 'WebMsgInfo/contents', 'required');
			$message .= $Valid->ErrorMsg($data, 'WebMsgInfo/contents', 'mbmaxlength', '3000');
		}
		
// 		if(!$Valid->required($data['WebMsgInfo']['contents']) || !$Valid->mbmaxlength($data['WebMsgInfo']['contents'],3000)){
// 			$this->htmlValid[]='txtArea';
// 		}
		if($data['status'] < 2 && isset($data['WebMsgInfo']['subject'])) {
			if (!$Valid->required($data['WebMsgInfo']['subject'])){
				$this->htmlValid[]='resubject';
			}
		}

		//質問のみの場合、内容必須チェック
		if (empty($data['status']) || $data['status']=='3') {
			if(!$Valid->required($data['WebMsgInfo']['contents'])){
				$this->htmlValid[]='txtArea';
			}
		}
		return $message;
	}
	
	function doDelete($id) {
		$sql = "update web_msg_info set member_deleted=now() " . " where id in (".Sanitize::sql($id).")";
		$this->query($sql);
	}
	
	
	function getWebMsgInfo($member_id,$scout_msg_id) {
		$sql = "select * from member_email_msg where member_id = ". Sanitize::sql($member_id)
				. " and srid = " . Sanitize::sql($scout_msg_id)
				. " ORDER BY received desc ";
		$msgRs = $this->query($sql);
		return $msgRs;
	}
	
	function getAllWebMsgInfo($member_id,$scout_msg_id) {
		$sql = "select * from member_email_msg where member_id = ". Sanitize::sql($member_id)
		. " and srid = " . Sanitize::sql($scout_msg_id)
		. " ORDER BY received desc ";
		$msgRs = $this->query($sql);
		return $msgRs;
	}
	
	function upStatus($id) {
		$sql = "update web_msg_info set status='1' " . " where id = ".Sanitize::sql($id) . " and status='0' ";
		$this->query($sql);
	}
	
// 	function upReturnComMsg($status,$id) {
// 		$sql = "update web_msg_info set scout_status='".Sanitize::sql($status)."' where id = ".Sanitize::sql($id);
// 		$this->query($sql);
// 	}
	
	function upReturnMsg($id) {
		$sql = "update web_msg_info set status='2' where id = ".Sanitize::sql($id);
		$this->query($sql);
	}
	
}

?>